import axios from 'axios';


export function lastIndexes(params){
    return new Promise((resolve, reject) => {
        axios({
            method: 'get',
            url: '/apis/lastIndexes',
            data: params
        }).then(response => {
            const processedData = extractAndTransform(response.data);   //[{ "Host": "node2", "Metric": "total_cpu_usage", "Time": 1717910534919, "FormattedTime": "2024/6/9 13:22:14", "Value": "4.749512036434613", "DataType": "DOUBLE" }
            console.log(processedData)
            resolve(processedData);
        }).catch(error => {
            reject(error);
        });
    });

}
function extractAndTransform(data) {
    const result = [];

    data.data.forEach(record => {
        const parts = record.TimeSeries.split('.');
        const vm_node = parts[2].replace("vm_",""); // e.g., vm_node0, vm_node1
        const metric = parts[3]; // e.g., mem_used, core_usages_23

        let vmObject = result.find(vm => vm.Host === vm_node);
        if (!vmObject) {
            vmObject = {
                Host: vm_node,
                Time: record.Time,
                TimeString: new Date(record.Time).toLocaleString(),
                core_usages: []
            };
            result.push(vmObject);
        }

        switch (metric) {
            case 'mem_used':
            case 'mem_available':
            case 'mem_total':
            case 'mem_free':
                vmObject[metric] = record.Value;
                break;
            case 'mem_used_percent':
            case 'total_cpu_usage':
                vmObject[metric] = parseFloat(record.Value).toFixed(2);
                break;
            default:
                if (metric.startsWith('core_usages_')) {
                    vmObject.core_usages.push(parseFloat(record.Value).toFixed(2));
                }
                break;
        }
    });

    return result;
}



export function getindexsByTimes(params){
    console.log("params",params)
    return new Promise((resolve, reject) => {
        axios({
            method: 'get',
            url: '/apis/getindexsByTimes',
            params: params
        }).then(response => {
            // console.log(response.data)
            resolve(getindexsByTimesProcessResponseData(response));
        }).catch(error => {
            reject(error);
        });
    });
}

function getindexsByTimesProcessResponseData(response) {
    const result = {};
    response.data.data.forEach(item => {
        const deviceName = item.Device.split('.').pop();
        if (!result[deviceName]) {
            result[deviceName] = [];
        }
        const formattedTime = new Date(item.Time).toLocaleString().replace('T', ' ').substring(0, 19);
        result[deviceName].push({
            Time: item.Time,
            Device: item.Device,
            total_cpu_usage: item.total_cpu_usage,
            mem_used_percent: item.mem_used_percent,
            TimeStr: formattedTime
        });
    });
    
    // 按 Time 升序排序
    for (const deviceName in result) {
        result[deviceName].sort((a, b) => new Date(a.Time) - new Date(b.Time));
    }
    
    return result;
}

